package cn.wolfcode.interceptor;

import cn.wolfcode.domain.Employee;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

// 登录拦截器
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    // 由servlet提供的原生拦截器
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 从session 获取当前登录用户
        Employee employee = (Employee) request.getSession().getAttribute("USER_IN_SESSION");
        // 判断是否为空
        if (employee == null) {
            // 空 ---> 重定向到登录页面,不放行
            response.sendRedirect("/login.html");
            return false;
        }
        // 不为空 --> 放行
        return true;
    }

}

